﻿var scene = null;
var renderer = null;
var camera = null;
var camControls = null;
var userheigh = 240;
var box = null;
var bigbox = null;
var doublebox = null;
var tribox = null;
var quabox = null;
var boxes = new THREE.Object3D();
var fixible = new THREE.Object3D();
var light = null;

var availableground = null;

// mouse choose item
var mouse = new THREE.Vector2(), hovereditem, ray;

var gui;
var controls = {
	"store": '模具仓库',
	"keyword": '',
	"query": function()
	{
		var keyword = controls["keyword"];
		if (keyword == "")
			return;
		if (selectedbox)
			selectedbox.material.color.setHex(0x2a5caa);
		selectedbox = null;
		gui.__controllers[gui.__controllers.length - 1].remove();
		controls["result"] = "";
		$.ajax({
			url:"../index.php/virtual/boxlocation?queryitem=" + keyword,
			type:"post",
			data:"",
			async:false,
			error:function()
			{
				gui.add(controls, "result", []).name("查询错误");
			},
			success:function(data)
			{
				var normalization = data.replace("[", "").replace("]", "");
				while(normalization.indexOf('"') >= 0)
					normalization = normalization.replace('"', '')
				var list = normalization.split(",");
				if (list.length > 0)
					controls["result"] = list[0];
				var guiresult = gui.add(controls, "result", list);
				guiresult.name("查询结果");
				guiresult.onChange(switchCamera);
				switchCamera();
			}
		});
	},
	"result": ''
};

function initUI()
{
	gui = new dat.GUI();
    // 显示仓库和查询框
    var guistore = gui.add(controls, "store", [ '模具仓库']);
	guistore.onChange(changeStore);
	guistore.name("选择仓库");
    gui.add(controls, "keyword", '').name("输入关键字");
    gui.add(controls, "query").name("------> 查询货架");
	var guiresult = gui.add(controls, "result", []);
	guiresult.name("查询结果");
	guiresult.onChange(switchCamera);
}

function changeStore() {
    if (this.store == "模具仓库")
        return;
    location.href = "rubberstore.html";
}

var selectedbox = null;

function switchCamera()
{
	var boxname = controls["result"];
	if (boxname == null || boxname == "")
		return;
	var cabinetname = boxname.split("-")[0];
	var intname = parseInt(cabinetname);
	for(var i = 0, l = fixible.children.length; i < l; i++)
	{
		var current = fixible.children[i];
		if (current.name == cabinetname)
		{
			camera.position.copy(current.position);
			camera.position.y = userheigh;
			if (intname <= 36)
			{
				camera.position.x = 400;
				camControls.lon = 0;
				camControls.lat = 120;
			}
			else if (intname <= 72)
			{
				camera.position.x = -400;
				camControls.lon = 180;
				camControls.lat = 120;
			}
			else if (intname <= 98)
			{
				if (intname % 2 == 0)
				{
					camera.position.x = -400;
					camControls.lon = 0;
					camControls.lat = 120;
				}
				else
				{
					camera.position.x = 400;
					camControls.lon = 180;
					camControls.lat = 120;
				}
			}
			else if (intname <=103)
			{
				camera.position.z = -3800;
				camControls.lon = 270;
				camControls.lat = 120;
			}
			else
			{
				camera.position.x = 0;
				camera.position.z = 1000;
			}
			break;
		}
	}
	var found = false;
	for(var i = 0, l = boxes.children.length; i < l; i++)
	{
		var current = boxes.children[i];
		if (current.name == boxname)
		{
			found = true;
			if (selectedbox != current)
			{
				if (selectedbox)
					selectedbox.material.color.setHex(0x2a5caa);
				selectedbox = current;
				selectedbox.material.color.setHex(0x00ff00);
			}
		}
	}
	if (found == false)
	{
		alert("格子：" + boxname + " 未找到。")
	}
}

function FilterCabinetBox()
{
	// var boxnamesfromdatabase = [];
	// for(int i = 0; i < boxes.length; ++i)
	// {
	// 	if (boxnamesfromdatabase.contains(boxes[i].name))
	// 		continue;
	// 	boxes[i].visible = false;
	// }
}

function onDocumentMouseMove(event) {
    event.preventDefault();
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}

function onDocumentMouseClick(event) {
	if (!hovereditem)
		return;
    if (!hovereditem.visible)
        return;
    event.preventDefault();
    window.open("../index.php/virtual/boxdetail?store=mouldstore&location=" + hovereditem.name);
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
}

function init() {
	initUI();
	
    // 人物移动需要的时钟
    var clock = new THREE.Clock();

    // 初始化场景
    scene = new THREE.Scene();
    CreateBox();

    var ambientLight = new THREE.AmbientLight(0x7f7f7f);
    scene.add(ambientLight);

    initScene();

    ray = new THREE.Raycaster();

    // 初始化webgl
    renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('scene'), antialias: true });
    renderer.setClearColor(new THREE.Color(0xEEEEEE));
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.sortObjects = false;
//    renderer.shadowMapEnabled = true;

    // 初始化人物视角
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.x = 0;
    camera.position.y = userheigh;
    camera.position.z = 1200;

    camControls = new THREE.FirstPersonControls(camera);
    camControls.lookSpeed = 0.1;
    camControls.movementSpeed = 900;
    camControls.noFly = true;
    camControls.lookVertical = false;
    camControls.constrainVertical = true;
    camControls.verticalMin = 1.59;
    camControls.verticalMax = 1.6;
    camControls.lon = -90;
    camControls.lat = 120;
    camControls.ground = availableground;
	
	addLight(-600, -4000);
	addLight(-600, -2500);
	addLight(-600, -1000);
	addLight(-600, 500);
	addLight(600, -4000);
	addLight(600, -2500);
	addLight(600, -1000);
	addLight(600, 900);
	
    light = new THREE.SpotLight(0xffffff);
    light.position.copy(camera.position);
    light.position.y = 6000;
    light.castShadow = true;
    light.target = camera;
    scene.add(light);

    document.addEventListener('mousemove', onDocumentMouseMove, false);
    document.addEventListener('click', onDocumentMouseClick, false);
    window.addEventListener('resize', onWindowResize, false);

    var render = function () {
        camControls.update(clock.getDelta());
        renderScene();
        requestAnimationFrame(render);
    };
    render();
}

function addLight(x, z)
{
	var fixedsphere = createMesh2(new THREE.SphereGeometry(1, 1, 1), 0xffffff);
	fixedsphere.position.set(x, 0, z)
    var fixedlight = new THREE.SpotLight(0xffffff);
    fixedlight.position.set(x, 1000, z);
    fixedlight.castShadow = true;
    fixedlight.target = fixedsphere;
    fixible.add(fixedsphere);
	fixible.add(fixedlight);
}

function CreateBox()
{
    box = new THREE.Geometry();
    var color = 0x74787c;
	
	var sphere = createMesh2(new THREE.SphereGeometry(12, 12, 12), color);
	sphere.position.set(0, 2, 0)
	sphere.updateMatrix();
	box.merge(sphere.geometry, sphere.matrix);
	
	var cube1 = createMesh2(new THREE.BoxGeometry(180, 5, 80), color);
	cube1.position.set(0, -10, 0)
	cube1.updateMatrix();
	box.merge(cube1.geometry, cube1.matrix);
	var cube2 = createMesh2(new THREE.BoxGeometry(30, 30, 30), color);
	cube2.position.set(10, 5, 20)
	cube2.updateMatrix();
	box.merge(cube2.geometry, cube2.matrix);
	var cube3 = createMesh2(new THREE.BoxGeometry(5, 5, 50), color);
	cube3.position.set(0, -7, -10)
	cube3.updateMatrix();
	box.merge(cube3.geometry, cube3.matrix);
	var cube4 = createMesh2(new THREE.BoxGeometry(110, 15, 15), color);
	cube4.position.set(-20, -5, -20)
	cube4.updateMatrix();
	box.merge(cube4.geometry, cube4.matrix);
	
	var cylinder = createMesh2(new THREE.CylinderGeometry(12, 12, 24, 18, 3), color);
	cylinder.position.set(-40, 5, 12)
	cylinder.updateMatrix();
	box.merge(cylinder.geometry, cylinder.matrix);


    doublebox = new THREE.Geometry();
    var color = 0x74787c;
	
	var sphere2 = createMesh2(new THREE.SphereGeometry(12, 12, 12), color);
	sphere2.position.set(0, 2, 0)
	sphere2.updateMatrix();
	doublebox.merge(sphere2.geometry, sphere2.matrix);
	
	var cube11 = createMesh2(new THREE.BoxGeometry(180, 5, 180), color);
	cube11.position.set(0, -10, 0)
	cube11.updateMatrix();
	doublebox.merge(cube11.geometry, cube11.matrix);
	var cube12 = createMesh2(new THREE.BoxGeometry(30, 30, 30), color);
	cube12.position.set(60, 5, 50)
	cube12.updateMatrix();
	doublebox.merge(cube12.geometry, cube12.matrix);
	var cube13 = createMesh2(new THREE.BoxGeometry(5, 5, 50), color);
	cube13.position.set(0, -7, -70)
	cube13.updateMatrix();
	doublebox.merge(cube13.geometry, cube13.matrix);
	var cube14 = createMesh2(new THREE.BoxGeometry(110, 15, 15), color);
	cube14.position.set(-60, -5, -50)
	cube14.updateMatrix();
	doublebox.merge(cube14.geometry, cube14.matrix);
	
	var cylinder1 = createMesh2(new THREE.CylinderGeometry(12, 12, 24, 18, 3), color);
	cylinder1.position.set(-70, 5, 72)
	cylinder1.updateMatrix();
	doublebox.merge(cylinder1.geometry, cylinder1.matrix);
	
	
    quabox = new THREE.Geometry();
    var color = 0x74787c;
	
	var sphere3 = createMesh2(new THREE.SphereGeometry(12, 12, 12), color);
	sphere3.position.set(0, 2, 0)
	sphere3.updateMatrix();
	quabox.merge(sphere3.geometry, sphere3.matrix);
	
	var cube31 = createMesh2(new THREE.BoxGeometry(380, 5, 380), color);
	cube31.position.set(0, -10, 0)
	cube31.updateMatrix();
	quabox.merge(cube31.geometry, cube31.matrix);
	var cube32 = createMesh2(new THREE.BoxGeometry(30, 30, 30), color);
	cube32.position.set(160, 5, 150)
	cube32.updateMatrix();
	quabox.merge(cube32.geometry, cube32.matrix);
	var cube33 = createMesh2(new THREE.BoxGeometry(5, 5, 50), color);
	cube33.position.set(0, -17, -170)
	cube33.updateMatrix();
	quabox.merge(cube33.geometry, cube33.matrix);
	var cube34 = createMesh2(new THREE.BoxGeometry(210, 15, 15), color);
	cube34.position.set(-160, -15, -150)
	cube34.updateMatrix();
	quabox.merge(cube34.geometry, cube34.matrix);
	
	var cylinder3 = createMesh2(new THREE.CylinderGeometry(12, 12, 24, 18, 3), color);
	cylinder3.position.set(-170, 5, 172)
	cylinder3.updateMatrix();
	quabox.merge(cylinder3.geometry, cylinder3.matrix);
	
	
    tribox = new THREE.Geometry();
    var color = 0x74787c;
	
	var sphere4 = createMesh2(new THREE.SphereGeometry(12, 12, 12), color);
	sphere4.position.set(0, 2, 0)
	sphere4.updateMatrix();
	tribox.merge(sphere4.geometry, sphere4.matrix);
	
	var cube41 = createMesh2(new THREE.BoxGeometry(380, 5, 180), color);
	cube41.position.set(0, -10, 0)
	cube41.updateMatrix();
	tribox.merge(cube41.geometry, cube41.matrix);
	var cube42 = createMesh2(new THREE.BoxGeometry(30, 30, 30), color);
	cube42.position.set(160, 5, 50)
	cube42.updateMatrix();
	tribox.merge(cube42.geometry, cube42.matrix);
	var cube43 = createMesh2(new THREE.BoxGeometry(5, 5, 50), color);
	cube43.position.set(0, 0, -70)
	cube43.updateMatrix();
	tribox.merge(cube43.geometry, cube43.matrix);
	var cube44 = createMesh2(new THREE.BoxGeometry(210, 15, 15), color);
	cube44.position.set(-60, 0, -50)
	cube44.updateMatrix();
	tribox.merge(cube44.geometry, cube44.matrix);
	
	var cylinder4 = createMesh2(new THREE.CylinderGeometry(12, 12, 24, 18, 3), color);
	cylinder4.position.set(-170, 5, 72)
	cylinder4.updateMatrix();
	tribox.merge(cylinder4.geometry, cylinder4.matrix);
}

function initScene()
{
    AddStore(600);

    AddCabinet(0, 900, -3500, 1, "left");
    AddCabinet(0, 700, -3500, 2, "left");
    AddCabinet(0, 900, -3400, 3, "left");
    AddCabinet(0, 700, -3400, 4, "left");
    AddCabinet(0, 900, -3100, 5, "left");
    AddCabinet(0, 700, -3100, 6, "left");
    AddCabinet(0, 900, -3000, 7, "left");
    AddCabinet(0, 700, -3000, 8, "left");
    AddCabinet(0, 900, -2700, 9, "left");
    AddCabinet(0, 700, -2700, 10, "left");
    AddCabinet(0, 900, -2600, 11, "left");
    AddCabinet(0, 700, -2600, 12, "left");
    AddCabinet(0, 900, -2300, 13, "left");
    AddCabinet(0, 700, -2300, 14, "left");
    AddCabinet(0, 900, -2200, 15, "left");
    AddCabinet(0, 700, -2200, 16, "left");
    AddCabinet(0, 900, -1900, 17, "left");
    AddCabinet(0, 700, -1900, 18, "left");
    AddCabinet(0, 900, -1800, 19, "left");
    AddCabinet(0, 700, -1800, 20, "left");
    AddCabinet(0, 900, -1500, 21, "left");
    AddCabinet(0, 700, -1500, 22, "left");
    AddCabinet(0, 900, -1400, 23, "left");
    AddCabinet(0, 700, -1400, 24, "left");
    AddCabinet(0, 900, -1100, 25, "left");
    AddCabinet(0, 700, -1100, 26, "left");
    AddCabinet(0, 900, -1000, 27, "left");
    AddCabinet(0, 700, -1000, 28, "left");
    AddCabinet(0, 900, -700, 29, "left");
    AddCabinet(0, 700, -700, 30, "left");
    AddCabinet(0, 900, -600, 31, "left");
    AddCabinet(0, 700, -600, 32, "left");
    AddCabinet(0, 900, -300, 33, "left");
    AddCabinet(0, 700, -300, 34, "left");
    AddCabinet(0, 900, -200, 35, "left");
    AddCabinet(0, 700, -200, 36, "left");
	
    AddCabinet(0, -700, -3500, 37, "right");
    AddCabinet(0, -900, -3500, 38, "right");
    AddCabinet(0, -700, -3400, 39, "right");
    AddCabinet(0, -900, -3400, 40, "right");
    AddCabinet(0, -700, -3100, 41, "right");
    AddCabinet(0, -900, -3100, 42, "right");
    AddCabinet(0, -700, -3000, 43, "right");
    AddCabinet(0, -900, -3000, 44, "right");
    AddCabinet(0, -700, -2700, 45, "right");
    AddCabinet(0, -900, -2700, 46, "right");
    AddCabinet(0, -700, -2600, 47, "right");
    AddCabinet(0, -900, -2600, 48, "right");
    AddCabinet(0, -700, -2300, 49, "right");
    AddCabinet(0, -900, -2300, 50, "right");
    AddCabinet(0, -700, -2200, 51, "right");
    AddCabinet(0, -900, -2200, 52, "right");
    AddCabinet(0, -700, -1900, 53, "right");
    AddCabinet(0, -900, -1900, 54, "right");
    AddCabinet(0, -700, -1800, 55, "right");
    AddCabinet(0, -900, -1800, 56, "right");
    AddCabinet(0, -700, -1500, 57, "right");
    AddCabinet(0, -900, -1500, 58, "right");
    AddCabinet(0, -700, -1400, 59, "right");
    AddCabinet(0, -900, -1400, 60, "right");
    AddCabinet(0, -700, -1100, 61, "right");
    AddCabinet(0, -900, -1100, 62, "right");
    AddCabinet(0, -700, -1000, 63, "right");
    AddCabinet(0, -900, -1000, 64, "right");
    AddCabinet(0, -700, -700, 65, "right");
    AddCabinet(0, -900, -700, 66, "right");
    AddCabinet(0, -700, -600, 67, "right");
    AddCabinet(0, -900, -600, 68, "right");
    AddCabinet(0, -700, -300, 69, "right");
    AddCabinet(0, -900, -300, 70, "right");
    AddCabinet(0, -700, -200, 71, "right");
    AddCabinet(0, -900, -200, 72, "right");

    AddDoubleCabinet(0, 100, -620 - 220 * 12, 73, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 12, 74, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 11, 75, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 11, 76, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 10, 77, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 10, 78, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 9, 79, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 9, 80, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 8, 81, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 8, 82, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 7, 83, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 7, 84, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 6, 85, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 6, 86, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 5, 87, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 5, 88, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 4, 89, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 4, 90, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 3, 91, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 3, 92, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 2, 93, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 2, 94, "left");
    AddDoubleCabinet(0, 100, -620 - 220 * 1, 95, "right");
    AddDoubleCabinet(0, -100, -620 - 220 * 1, 96, "left");
    AddDoubleCabinet(0, 100, -620, 97, "right");
    AddDoubleCabinet(0, -100, -620, 98, "left");

    AddQuaCabinet(0, -800, -4400, 99);
    AddQuaCabinet(0, -400, -4400, 100);
    AddQuaCabinet(0, 0, -4400, 101);
    AddQuaCabinet(0, 400, -4400, 102);
    AddQuaCabinet(0, 800, -4400, 103);
	
	AddTriCabinet(0, 400, 250, 152, "left");
	AddTriCabinet(0, 800, 250, 153, "left");
	AddTriCabinet(0, 500, 600, 155, "left");
	AddTriCabinet(0, 500, 1000, 156, "left");
	AddTriCabinet(0, 500, 1400, 158, "left");
	AddTriCabinet(0, 500, 1800, 159, "left");

	AddTriCabinet(Math.PI * 3/ 2, 800, 700, 154, "left");
	AddTriCabinet(Math.PI * 3/ 2, 800, 1300, 157, "left");

	FilterCabinetBox();
    scene.add(fixible);
    scene.add(boxes);
}

function AddCabinet(rotate, x, z, code, title) {
    var cabinet = createCabinet();
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
    cabinet.castShadow = true;
    fixible.add(cabinet);

	AddBoxesForCabinet(x, z, code);

    if (title != "left" && title != "right")
        return;
    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 35, height: 4 }), 0x5c7a29);
    if (title == "right")
    {
        text.position.set(x + 98, 292, z - 12);
        text.rotation.y = Math.PI / 2;
    }
    if (title == "left")
    {
        text.position.set(x - 98, 292, z - 88);
        text.rotation.y = Math.PI * 3 / 2;
    }
    text.updateMatrix();
    fixible.add(text);
}

function AddDoubleCabinet(rotate, x, z, code, title) {
    var cabinet = createDoubleCabinet();
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
    cabinet.castShadow = true;
    fixible.add(cabinet);

	AddDoubleBoxesForCabinet(x, z, code);

    if (title != "left" && title != "right")
        return;
    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 35, height: 4 }), 0x5c7a29);
    if (title == "right")
    {
        text.position.set(x + 98, 292, z - 62);
        text.rotation.y = Math.PI / 2;
    }
    if (title == "left")
    {
        text.position.set(x - 98, 292, z - 138);
        text.rotation.y = Math.PI * 3 / 2;
    }
    text.updateMatrix();
    fixible.add(text);
}

function AddQuaCabinet(rotate, x, z, code) {
    var cabinet = createQuaCabinet();
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
    cabinet.castShadow = true;
    fixible.add(cabinet);

	AddQuaBoxesForCabinet(x, z, code);

    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 35, height: 4 }), 0x5c7a29);
    text.position.set(x  - 100, 292, z + 2);
    text.rotation.y = 0;
    text.updateMatrix();
    fixible.add(text);
}

function AddTriCabinet(rotate, x, z, code, title) {
    var cabinet = createTriCabinet();
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
	cabinet.rotation.y = rotate;
    cabinet.castShadow = true;
    fixible.add(cabinet);

	AddTriBoxesForCabinet(rotate, x, z, code);

    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 35, height: 4 }), 0x5c7a29);
    if (title == "right")
    {
        text.position.set(x + 198, 292, z - 62);
        text.rotation.y = Math.PI / 2 + rotate;
		if (rotate != 0)
		{
			text.position.set(x,292, z);
		}
    }
    if (title == "left")
    {
        text.position.set(x - 198, 292, z - 138);
        text.rotation.y = Math.PI * 3 / 2 + rotate;
		if (rotate != 0)
		{
			text.position.set(x + 138,292, z - 198);
		}
    }
    text.updateMatrix();
    fixible.add(text);
}


function AddBoxesForCabinet(x, z, code) {
    AddBox(x, 10, z, code + "-1");
    AddBox(x, 50, z, code + "-2");
    AddBox(x, 90, z, code + "-3");
    AddBox(x, 130, z, code + "-4");
    AddBox(x, 170, z, code + "-5");
    AddBox(x, 210, z, code + "-6");
    AddBox(x, 250, z, code + "-7");
}

function AddDoubleBoxesForCabinet(x, z, code) {
    AddDoubleBox(x, 10, z, code + "-1");
    AddDoubleBox(x, 50, z, code + "-2");
    AddDoubleBox(x, 90, z, code + "-3");
    AddDoubleBox(x, 130, z, code + "-4");
    AddDoubleBox(x, 170, z, code + "-5");
    AddDoubleBox(x, 210, z, code + "-6");
    AddDoubleBox(x, 250, z, code + "-7");
}

function AddQuaBoxesForCabinet(x, z, code) {
    AddQuaBoxe(x, 10, z, code + "-1");
    AddQuaBoxe(x, 50, z, code + "-2");
    AddQuaBoxe(x, 90, z, code + "-3");
    AddQuaBoxe(x, 130, z, code + "-4");
    AddQuaBoxe(x, 170, z, code + "-5");
    AddQuaBoxe(x, 210, z, code + "-6");
    AddQuaBoxe(x, 250, z, code + "-7");
}

function AddTriBoxesForCabinet(rotate, x, z, code) {
    AddTriBoxe(rotate, x, 10, z, code + "-1");
    AddTriBoxe(rotate, x, 50, z, code + "-2");
    AddTriBoxe(rotate, x, 90, z, code + "-3");
    AddTriBoxe(rotate, x, 130, z, code + "-4");
    AddTriBoxe(rotate, x, 170, z, code + "-5");
    AddTriBoxe(rotate, x, 210, z, code + "-6");
    AddTriBoxe(rotate, x, 250, z, code + "-7");
}

function AddBox(x, y, z, code) {

    var object = createMesh(box, 0x2a5caa);
    object.position.set(x, y + 15, z - 50);
    object.name = code;
    boxes.add(object);
}

function AddDoubleBox(x, y, z, code) {

    var object = createMesh(doublebox, 0x2a5caa);
    object.position.set(x, y + 15, z - 100);
    object.name = code;
    boxes.add(object);
}

function AddQuaBoxe(x, y, z, code) {

    var object = createMesh(quabox, 0x2a5caa);
    object.position.set(x, y + 15, z - 200);
    object.name = code;
    boxes.add(object);
}

function AddTriBoxe(rotate, x, y, z, code) {

    var object = createMesh(tribox, 0x2a5caa);
    object.position.set(x, y + 15, z - 100);
	object.rotation.y = rotate;
	if (rotate != 0)
	{
		object.position.set(x + 100, y + 15, z);
	}
    object.name = code;
    boxes.add(object);
}

function renderScene() {
    light.position.copy(camera.position);
    light.position.y = 1000;
    camera.position.y = userheigh;
    camera.updateMatrixWorld();

    // for (var i = 0, l = boxes.children.length; i < l; i++)
    // {
        // var currentbox = boxes.children[i];
        // var distancex = camera.position.x - currentbox.position.x;
        // var distancez = camera.position.z - currentbox.position.z;
        // var distance = distancex * distancex + distancez * distancez;
        // currentbox.visible = distance < 64000000;
        // if (currentbox.visible && !boxtexts.containsKey(currentbox.name))
        // {
            // var boxtext = createMesh2(new THREE.TextGeometry(currentbox.name, { size: 5, height: 2 }), 0x2a5caa);
            // boxtext.position.set(currentbox.position.x - 25, currentbox.position.y -15, currentbox.position.z + 25);
            // boxtext.rotation.y = currentbox.rotation.y;
            // fixible.add(boxtext);
            // boxtexts.add(currentbox.name, boxtext);
        // }
        // else if (!currentbox.visible && boxtexts.containsKey(currentbox.name))
        // {
            // fixible.remove(boxtexts.getValue(currentbox.name));
            // boxtexts.remove(currentbox.name);
        // }
    // }

    ray.setFromCamera(mouse, camera);
    var intersects = ray.intersectObjects(boxes.children);
    if (intersects.length > 0) {
        if (hovereditem != intersects[0].object) {
            if (hovereditem)
                hovereditem.material.emissive.setHex(hovereditem.currentHex);
            hovereditem = intersects[0].object;
            hovereditem.currentHex = hovereditem.material.emissive.getHex();
            hovereditem.material.emissive.setHex(0xff0000);
        }
    }
    else {
        if (hovereditem)
            hovereditem.material.emissive.setHex(hovereditem.currentHex);
        hovereditem = null;
    }
    renderer.render(scene, camera);
}

function AddStore(height) {

    var wallcolor = 0x96582a;

    var left = createMesh2(new THREE.BoxGeometry(1, height, 6600), wallcolor);
    left.position.set(-1000, height / 2, -1500);
    fixible.add(left);

    var right = createMesh2(new THREE.BoxGeometry(1, height, 6600), wallcolor);
    right.position.set(1000, height / 2, -1500);
    fixible.add(right);

    var front = createMesh2(new THREE.BoxGeometry(2000, height, 1), wallcolor);
    front.position.set(0, height / 2, -4800);
    fixible.add(front);

    var back = createMesh2(new THREE.BoxGeometry(2000, height, 1), wallcolor);
    back.position.set(0, height / 2, 1800);
    fixible.add(back);

    var groundcolor = 0xf2eada;
    var ground = createMesh2(new THREE.BoxGeometry(2000, 1, 6600), groundcolor);
    ground.position.set(0, 0, -1500);
    ground.receiveShadow = true;
    fixible.add(ground);

    // office
    var office = createMesh2(new THREE.TextGeometry("OFFICE", { size: 80, height: 2 }), 0x411445);
    office.position.set(-600, height / 2, 1500);
    office.rotation.y = Math.PI / 2;
    fixible.add(office);
    var office11 = createMesh2(new THREE.BoxGeometry(400, height, 1), wallcolor);
    office11.position.set(-800, height / 2, 1000);
    fixible.add(office11);
    var office12 = createMesh2(new THREE.BoxGeometry(1, height, 800), wallcolor);
    office12.position.set(-600, height / 2, 1400);
    fixible.add(office12);
	
    // guan qiang
    var ka1 = createMesh2(new THREE.BoxGeometry(800, height, 100), wallcolor);
    ka1.position.set(600, height / 2, 0);
    fixible.add(ka1);
    var ka2 = createMesh2(new THREE.BoxGeometry(800, height, 100), wallcolor);
    ka2.position.set(-600, height / 2, 0);
    fixible.add(ka2);

    // sheng jiang ji
    var lifter = createMesh2(new THREE.BoxGeometry(400, 2, 400), 0x8B0000);
    lifter.position.set(0, 0, 1600);
    fixible.add(lifter);
    var liftertext = createMesh2(new THREE.TextGeometry("LIFTER", { size: 80, height: 2 }), 0x411445);
    liftertext.position.set(-150, 0, 1600);
    liftertext.rotation.x = Math.PI * 3 / 2;
    fixible.add(liftertext);

    // caozuo tai
    var operation = createMesh2(new THREE.BoxGeometry(600, 2, 800), 0x8B0000);
    operation.position.set(-650, 0, 500);
    fixible.add(operation);
    var operation1 = createMesh2(new THREE.TextGeometry("OPERATION", { size: 70, height: 2 }), 0x411445);
    operation1.position.set(-900, 0, 400);
    operation1.rotation.x = Math.PI * 3 / 2;
    fixible.add(operation1);
    var operation2 = createMesh2(new THREE.TextGeometry("PLATFORM", { size: 70, height: 2 }), 0x411445);
    operation2.position.set(-900, 0, 600);
    operation2.rotation.x = Math.PI * 3 / 2;
    fixible.add(operation2);

    // cars
    var car1 = createMesh2(new THREE.BoxGeometry(300, 250, 150), 0x8B0000);
    car1.position.set(850, 125, -75);
    fixible.add(car1);
    var car2 = createMesh2(new THREE.BoxGeometry(300, 250, 150), 0x8B0000);
    car2.position.set(850, 125, -4300);
    fixible.add(car2);


    // safe way
    var leftsafe = createMesh2(new THREE.BoxGeometry(400, 2, 3600), 0xfcaf17);
    leftsafe.position.set(-400, 0, -1800);
    fixible.add(leftsafe);
    var rightsafe = createMesh2(new THREE.BoxGeometry(400, 2, 3600), 0xfcaf17);
    rightsafe.position.set(400, 0, -1800);
    fixible.add(rightsafe);
	
    var centersafe1 = createMesh2(new THREE.BoxGeometry(2000, 2, 400), 0xfcaf17);
    centersafe1.position.set(0, 0, -3800);
    fixible.add(centersafe1);
    var centersafe2 = createMesh2(new THREE.BoxGeometry(400, 2, 1800), 0xfcaf17);
    centersafe2.position.set(0, 0, 500);
    fixible.add(centersafe2);
	
    var safe1 = createMesh2(new THREE.BoxGeometry(400, 2, 800), 0xfcaf17);
    safe1.position.set(-400, 0, 1400);
    fixible.add(safe1);
    var safe2 = createMesh2(new THREE.BoxGeometry(100, 2, 1400), 0xfcaf17);
    safe2.position.set(250, 0, 1100);
    fixible.add(safe2);

    availableground = [{ x: 998, y: 1798 }, { x: 998, y: 52 }, { x: 198, y: 52 },
              { x: 198, y: -52 }, { x: 998, y: -52 },
              { x: 998, y: -4798 }, { x: -998, y: -4798 },
              { x: -998, y: -52 }, { x: -198, y: -52 }, { x: -198, y: 52 },
              { x: -998, y: 52 },
              { x: -998, y: 998 }, { x: -598, y: 998 }, { x: -598, y: 1798 },
              { x: -198, y: 1798 }, { x: -198, y: 1398 }, { x: 202, y: 1398 },
              { x: 202, y: 1798 } ];
}

function createCabinet() {
    var x = 200;
    var y = 40 * 7 + 10; // 上下7排，间隔40
    var z = 100;

    var geometry = new THREE.Geometry();
    var color = 0x74787c;

    var left1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var left3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var left4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    left1.position.set(-x / 2, y, -z / 2);
    left3.position.set(-x / 2, y / 2, -z);
    left4.position.set(-x / 2, y / 2, 0);
    var right1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var right3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var right4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    right1.position.set(x / 2, y, -z / 2);
    right3.position.set(x / 2, y / 2, -z);
    right4.position.set(x / 2, y / 2, 0);

    left1.updateMatrix();
    left3.updateMatrix();
    left4.updateMatrix();
    right1.updateMatrix();
    right3.updateMatrix();
    right4.updateMatrix();
    geometry.merge(left1.geometry, left1.matrix);
    geometry.merge(left3.geometry, left3.matrix);
    geometry.merge(left4.geometry, left4.matrix);
    geometry.merge(right1.geometry, right1.matrix);
    geometry.merge(right3.geometry, right3.matrix);
    geometry.merge(right4.geometry, right4.matrix);

    var bottom = createMesh2(new THREE.BoxGeometry(x, 8, z), color);
    var h1 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h2 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h3 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h4 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h5 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h6 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);

    bottom.position.set(0, 10, -z / 2);
    h1.position.set(0, 50, -z / 2);
    h2.position.set(0, 90, -z / 2);
    h3.position.set(0, 130, -z / 2);
    h4.position.set(0, 170, -z / 2);
    h5.position.set(0, 210, -z / 2);
    h6.position.set(0, 250, -z / 2);

    bottom.updateMatrix();
    geometry.merge(bottom.geometry, bottom.matrix);
    h1.updateMatrix();
    geometry.merge(h1.geometry, h1.matrix);
    h2.updateMatrix();
    geometry.merge(h2.geometry, h2.matrix);
    h3.updateMatrix();
    geometry.merge(h3.geometry, h3.matrix);
    h4.updateMatrix();
    geometry.merge(h4.geometry, h4.matrix);
    h5.updateMatrix();
    geometry.merge(h5.geometry, h5.matrix);
    h6.updateMatrix();
    geometry.merge(h6.geometry, h6.matrix);

    return new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: color }));
}

function createDoubleCabinet() {
    var x = 200;
    var y = 40 * 7 + 10; // 上下7排，间隔40
    var z = 200;

    var geometry = new THREE.Geometry();
    var color = 0x74787c;

    var left1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var left3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var left4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    left1.position.set(-x / 2, y, -z / 2);
    left3.position.set(-x / 2, y / 2, -z);
    left4.position.set(-x / 2, y / 2, 0);
    var right1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var right3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var right4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    right1.position.set(x / 2, y, -z / 2);
    right3.position.set(x / 2, y / 2, -z);
    right4.position.set(x / 2, y / 2, 0);

    left1.updateMatrix();
    left3.updateMatrix();
    left4.updateMatrix();
    right1.updateMatrix();
    right3.updateMatrix();
    right4.updateMatrix();
    geometry.merge(left1.geometry, left1.matrix);
    geometry.merge(left3.geometry, left3.matrix);
    geometry.merge(left4.geometry, left4.matrix);
    geometry.merge(right1.geometry, right1.matrix);
    geometry.merge(right3.geometry, right3.matrix);
    geometry.merge(right4.geometry, right4.matrix);

    var bottom = createMesh2(new THREE.BoxGeometry(x, 8, z), color);
    var h1 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h2 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h3 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h4 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h5 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h6 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);

    bottom.position.set(0, 10, -z / 2);
    h1.position.set(0, 50, -z / 2);
    h2.position.set(0, 90, -z / 2);
    h3.position.set(0, 130, -z / 2);
    h4.position.set(0, 170, -z / 2);
    h5.position.set(0, 210, -z / 2);
    h6.position.set(0, 250, -z / 2);

    bottom.updateMatrix();
    geometry.merge(bottom.geometry, bottom.matrix);
    h1.updateMatrix();
    geometry.merge(h1.geometry, h1.matrix);
    h2.updateMatrix();
    geometry.merge(h2.geometry, h2.matrix);
    h3.updateMatrix();
    geometry.merge(h3.geometry, h3.matrix);
    h4.updateMatrix();
    geometry.merge(h4.geometry, h4.matrix);
    h5.updateMatrix();
    geometry.merge(h5.geometry, h5.matrix);
    h6.updateMatrix();
    geometry.merge(h6.geometry, h6.matrix);

    return new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: color }));
}

function createQuaCabinet() {
    var x = 400;
    var y = 40 * 7 + 10; // 上下7排，间隔40
    var z = 400;

    var geometry = new THREE.Geometry();
    var color = 0x74787c;

    var left1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var left3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var left4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    left1.position.set(-x / 2, y, -z / 2);
    left3.position.set(-x / 2, y / 2, -z);
    left4.position.set(-x / 2, y / 2, 0);
    var right1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var right3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var right4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    right1.position.set(x / 2, y, -z / 2);
    right3.position.set(x / 2, y / 2, -z);
    right4.position.set(x / 2, y / 2, 0);

    left1.updateMatrix();
    left3.updateMatrix();
    left4.updateMatrix();
    right1.updateMatrix();
    right3.updateMatrix();
    right4.updateMatrix();
    geometry.merge(left1.geometry, left1.matrix);
    geometry.merge(left3.geometry, left3.matrix);
    geometry.merge(left4.geometry, left4.matrix);
    geometry.merge(right1.geometry, right1.matrix);
    geometry.merge(right3.geometry, right3.matrix);
    geometry.merge(right4.geometry, right4.matrix);

    var bottom = createMesh2(new THREE.BoxGeometry(x, 8, z), color);
    var h1 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h2 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h3 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h4 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h5 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h6 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);

    bottom.position.set(0, 10, -z / 2);
    h1.position.set(0, 50, -z / 2);
    h2.position.set(0, 90, -z / 2);
    h3.position.set(0, 130, -z / 2);
    h4.position.set(0, 170, -z / 2);
    h5.position.set(0, 210, -z / 2);
    h6.position.set(0, 250, -z / 2);

    bottom.updateMatrix();
    geometry.merge(bottom.geometry, bottom.matrix);
    h1.updateMatrix();
    geometry.merge(h1.geometry, h1.matrix);
    h2.updateMatrix();
    geometry.merge(h2.geometry, h2.matrix);
    h3.updateMatrix();
    geometry.merge(h3.geometry, h3.matrix);
    h4.updateMatrix();
    geometry.merge(h4.geometry, h4.matrix);
    h5.updateMatrix();
    geometry.merge(h5.geometry, h5.matrix);
    h6.updateMatrix();
    geometry.merge(h6.geometry, h6.matrix);

    return new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: color }));
}

function createTriCabinet() {
    var x = 400;
    var y = 40 * 7 + 10; // 上下7排，间隔40
    var z = 200;

    var geometry = new THREE.Geometry();
    var color = 0x74787c;

    var left1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var left3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var left4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    left1.position.set(-x / 2, y, -z / 2);
    left3.position.set(-x / 2, y / 2, -z);
    left4.position.set(-x / 2, y / 2, 0);
    var right1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var right3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var right4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    right1.position.set(x / 2, y, -z / 2);
    right3.position.set(x / 2, y / 2, -z);
    right4.position.set(x / 2, y / 2, 0);

    left1.updateMatrix();
    left3.updateMatrix();
    left4.updateMatrix();
    right1.updateMatrix();
    right3.updateMatrix();
    right4.updateMatrix();
    geometry.merge(left1.geometry, left1.matrix);
    geometry.merge(left3.geometry, left3.matrix);
    geometry.merge(left4.geometry, left4.matrix);
    geometry.merge(right1.geometry, right1.matrix);
    geometry.merge(right3.geometry, right3.matrix);
    geometry.merge(right4.geometry, right4.matrix);

    var bottom = createMesh2(new THREE.BoxGeometry(x, 8, z), color);
    var h1 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h2 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h3 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h4 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h5 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h6 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);

    bottom.position.set(0, 10, -z / 2);
    h1.position.set(0, 50, -z / 2);
    h2.position.set(0, 90, -z / 2);
    h3.position.set(0, 130, -z / 2);
    h4.position.set(0, 170, -z / 2);
    h5.position.set(0, 210, -z / 2);
    h6.position.set(0, 250, -z / 2);

    bottom.updateMatrix();
    geometry.merge(bottom.geometry, bottom.matrix);
    h1.updateMatrix();
    geometry.merge(h1.geometry, h1.matrix);
    h2.updateMatrix();
    geometry.merge(h2.geometry, h2.matrix);
    h3.updateMatrix();
    geometry.merge(h3.geometry, h3.matrix);
    h4.updateMatrix();
    geometry.merge(h4.geometry, h4.matrix);
    h5.updateMatrix();
    geometry.merge(h5.geometry, h5.matrix);
    h6.updateMatrix();
    geometry.merge(h6.geometry, h6.matrix);

    return new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: color }));
}


function createMesh(geometry, colorvalue) {
    var meterial = new THREE.MeshPhongMaterial({ color: colorvalue });
    meterial.side = THREE.DoubleSide;
    return new THREE.Mesh(geometry, meterial);
}

function createMesh2(geometry, colorvalue)
{
    var meterial = new THREE.MeshPhongMaterial({ color: colorvalue });
    meterial.side = THREE.DoubleSide;
    return new THREE.Mesh(geometry, meterial);
}
